package blog.controller;

import blog.entity.Tag;
import blog.service.back.BlogBackService;
import blog.service.back.TagBackService;
import blog.util.back.ControllerUtil;
import blog.util.back.SensitiveWordFiltering;
import blog.vo.back.*;
import blog.vo.common.PaginationVO;
import blog.vo.common.ResponseListObjectVO;
import blog.vo.common.ResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamSource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.*;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 　　* @description: TODO
 * 　　* @param
 * 　　* @return
 * 　　* @throws
 * 　　* @author Administrator
 * 　　* @date $ $
 */
@Controller
@RequestMapping("/blog")
public class BlogController {
    @Autowired
    private BlogBackService blogBackService;

    @Autowired
    private TagBackService tagBackService;

    @RequestMapping("/get-all-blogInfo")
    public String getAllBlogInfo(PaginationVO paginationVO, Model model) {
        List<BlogVO> allNoneHighlightBlogInfo = blogBackService.getAllBlogInfo(paginationVO);
        model.addAttribute("allNoneHighlightBlogInfo", new ResponseListObjectVO<BlogVO>(200, "ok", allNoneHighlightBlogInfo).getResponseObject());
        return "home";
    }

    @RequestMapping("/blog-add")
    public String blogAdd(PaginationVO paginationVO, Model model) {
        List<Tag> allTag = tagBackService.getAllTag(paginationVO);
        model.addAttribute("allTag", new ResponseListObjectVO<Tag>(200, "ok", allTag).getResponseObject());
        return "blog-add";
    }

    @RequestMapping("/get-all-highlight-blogInfo")
    public String getAllHighlightBlogInfo(PaginationVO paginationVO, Model model) {
        List<BlogVO> allHighlightBlogInfo = blogBackService.getAllHighlightBlogInfo(paginationVO);
        model.addAttribute("allNoneHighlightBlogInfo", new ResponseListObjectVO<BlogVO>(200, "ok", allHighlightBlogInfo).getResponseObject());
        return "home";
    }

    @PostMapping("/by-highlight-get-blogInfo")
    public String byHighlightGetBlogInfo(ByHighlightGetBlogInfoVO byHighlightGetBlogInfoVO, Model model, HttpSession session) {
        List<BlogVO> allHighlightBlogInfo = blogBackService.byHighlightGetBlogInfo(byHighlightGetBlogInfoVO);
        model.addAttribute("allNoneHighlightBlogInfo", new ResponseListObjectVO<BlogVO>(200, "ok", allHighlightBlogInfo).getResponseObject());
        session.setAttribute("highlight", byHighlightGetBlogInfoVO.getHighlight());
        return "home";
    }

    @PostMapping("/by-blog-highlight-and-status-get-blogInfo")
    public String byBlogHighlightAndStatusGetBlogInfo(ByBlogHighlightAndStatusGetBlogInfo blogStatusGetBlogInfoVO, Model model, HttpSession session) {
        Integer highlight = (Integer) session.getAttribute("highlight");
        blogStatusGetBlogInfoVO.setHighlight(highlight);
        List<BlogVO> allHighlightBlogInfo = blogBackService.byBlogHighlightAndStatusGetBlogInfo(blogStatusGetBlogInfoVO);
        model.addAttribute("allNoneHighlightBlogInfo", new ResponseListObjectVO<BlogVO>(200, "ok", allHighlightBlogInfo).getResponseObject());
        return "home";
    }

    @RequestMapping("/get-all-root-highlight-blogInfo")
    public String getAllRootHighlightBlogInfo(PaginationVO paginationVO, Model model) {
        List<BlogVO> allRootHighlightBlogInfo = blogBackService.getAllRootHighlightBlogInfo(paginationVO);
        model.addAttribute("allNoneHighlightBlogInfo", new ResponseListObjectVO<BlogVO>(200, "ok", allRootHighlightBlogInfo).getResponseObject());
        return "home";
    }

    @RequestMapping("/like-blog-title-get-all-none-highlight-blogInfo")
    public String likeBlogTitleGetAllNoneHighlightBlogInfo(LikeBlogTitleGetAllNoneHighlightBlogInfoVO likeBlogTitleGetAllNoneHighlightBlogInfoVO, Model model) {
        List<BlogVO> blogVOS = blogBackService.likeBlogTitleGetAllNoneHighlightBlogInfo(likeBlogTitleGetAllNoneHighlightBlogInfoVO);
        model.addAttribute("allNoneHighlightBlogInfo", new ResponseListObjectVO<BlogVO>(200, "ok", blogVOS).getResponseObject());
        return "";
    }

    @RequestMapping("/by-tag-uuid-get-none-highlight-blogInfo")
    public String byTagUuidGetNoneHighlightBlogInfo(ByTagUuidGetNoneHighlightBlogInfoVO byTagUuidGetNoneHighlightBlogInfoVO, Model model) {
        List<BlogVO> blogVOS = blogBackService.byTagUuidGetNoneHighlightBlogInfo(byTagUuidGetNoneHighlightBlogInfoVO);
        model.addAttribute("allNoneHighlightBlogInfo", new ResponseListObjectVO<BlogVO>(200, "ok", blogVOS).getResponseObject());
        return "";
    }

    @RequestMapping("/by-blog-uuid-get-none-highlight-blogInfo")
    public String byBlogUuidGetNoneHighlightBlogInfo(ByBlogUuidGetNoneHighlightBlogInfoVO byBlogUuidGetNoneHighlightBlogInfoVO, Model model) {
        BlogVO blogVO = blogBackService.byBlogUuidGetNoneHighlightBlogInfo(byBlogUuidGetNoneHighlightBlogInfoVO);
        model.addAttribute("byBlogUuidGetNoneHighlightBlogInfo", new ResponseVO<BlogVO>(200, "ok", blogVO).getResponseObject());
        return "blog";
    }

    @PostMapping("/add-root-highlight-blog")
    public String addRootHighlightBlog(AddRootHighlightBlogVO addRootHighlightBlogVO, Model model,HttpSession session) {
        if (addRootHighlightBlogVO.getBlogTitle() == "" || addRootHighlightBlogVO.getBlogContent() == "" || addRootHighlightBlogVO.getTagUuid() == "") {
            String addResultUrl = ControllerUtil.addResultUrl(0, "redirect:/blog/get-all-root-highlight-blogInfo", model);
            return addResultUrl;
        }

        String blogTitle = replaceContent(addRootHighlightBlogVO.getBlogTitle());
        String blogContent = replaceContent(addRootHighlightBlogVO.getBlogContent());
        addRootHighlightBlogVO.setBlogTitle(blogTitle);
        addRootHighlightBlogVO.setBlogContent(blogContent);

        String blogUuid = blogBackService.addRootHighlightBlog(addRootHighlightBlogVO);
        session.setAttribute("blogUuid",blogUuid);
        return "redirect:/blog/blog-add";
    }

    @PostMapping("/by-blog-uuid-update-blogInfo")
    public String byBlogUuidUpdateBlogInfo(ByBlogUuidUpdateBlogInfoVO byBlogUuidUpdateBlogInfoVO, Model model) {
        int result = blogBackService.byBlogUuidUpdateBlogInfo(byBlogUuidUpdateBlogInfoVO);
        String updateResultUrl = ControllerUtil.updateResultUrl(result, "redirect:/blog/get-all-blogInfo", model);
        return updateResultUrl;
    }

    @RequestMapping("/by-blog-uuid-update-blog-status")
    public String byBlogUuidUpdateBlogStatus(ByBlogUuidUpdateBlogStatusVO byBlogUuidUpdateBlogStatusVO) {
        blogBackService.byBlogUuidUpdateBlogStatus(byBlogUuidUpdateBlogStatusVO);
        return "";
    }

    private String replaceContent(String content) {
        InputStream inputStream = BlogController.class.getClassLoader().getResourceAsStream("mg.properties");
        String newContent = "";
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            String[] split = br.readLine().split(",");
            for (int i = 0; i < split.length; i++) {
                Pattern r = Pattern.compile(split[i]);
                Matcher m = r.matcher(content);
                if (m.find()) {
                    newContent += m.replaceAll("*");
                }
            }
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
        return newContent;
    }

}
